package com.sxmmall.product.service.impl;

import com.sxmmall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxmmall.common.utils.PageUtils;
import com.sxmmall.common.utils.Query;

import com.sxmmall.product.dao.CategoryDao;
import com.sxmmall.product.entity.CategoryEntity;
import com.sxmmall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeMenusByIds(List<Long> asList) {

        // TODO 检查分类是否引用
        baseMapper.deleteBatchIds(asList);
        // 启用逻辑删除
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 查出所有的分类
        List<CategoryEntity> menuList = baseMapper.selectList(null);
        List<CategoryEntity> level1Menus = menuList.stream().filter(item -> item.getParentCid() == 0).map(menu ->{
            menu.setChildren(getChildrenList(menu,menuList));
            return menu;
        }).sorted((m1,m2) -> (m1.getSort()==null?0:m1.getSort()) - (m2.getSort()==null?0:m2.getSort())).collect(Collectors.toList());
        return level1Menus;
    }


    // 递归查找所有的子菜单
    private List<CategoryEntity> getChildrenList(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(item ->item.getParentCid() == root.getCatId()).map(menu ->{
            // 递归查询子菜单
            menu.setChildren(getChildrenList(menu,all));
            return menu;
        }).sorted((m1,m2) ->(m1.getSort()==null?0:m1.getSort()) - (m2.getSort()==null?0:m2.getSort())).collect(Collectors.toList());
        return children;
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        Collections.reverse(findParentPath( catelogId,paths));
        return paths.toArray(new Long[paths.size()]);
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths){
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0){
            findParentPath( byId.getParentCid(),paths);
        }
        return paths;
    }

    @Override
    @Transactional
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        if (!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        }
    }
}